package com.xinlu.common.game.action;

import java.lang.reflect.InvocationTargetException;

import com.xinlu.common.game.bo.BankBackBo;
import com.xinlu.common.game.bo.TzjlBo;
import com.xinlu.common.game.form.TzmxForm;
import com.xinlu.common.game.value.TzjlVo;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.util.tool.DateUtil;

public class TzmxAction extends BaseAction {

	private TzjlBo tzjlBo;
	private BankBackBo bankBackBo;
	private UsersBo usersBo;

	TzmxForm fr = new TzmxForm();

	public void setTzjlBo(TzjlBo tzjlBo) {
		this.tzjlBo = tzjlBo;
	}

	public void setFr(TzmxForm fr) {
		this.fr = fr;
	}
	
	public void setBankBackBo(BankBackBo bankBackBo) {
		this.bankBackBo = bankBackBo;
	}

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {
	}

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 加载页面
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String loadPage() throws IllegalAccessException, InvocationTargetException {

		TzjlVo vo = tzjlBo.loadTzmx(fr.getDzh());
		fr.setBs(vo.getBs());
		fr.setDgsj(DateUtil.getFormatDate(DateUtil.YYYY_MM_DD_HH_MI_SS, vo.getDgsj()));
		fr.setDqje(vo.getDqje());
		fr.setDzzt(vo.getDzzt());
		fr.setNfcd(vo.getNfcd());
		fr.setQh(vo.getQh());
		fr.setTzhm(vo.getTzhm());
		fr.setWfjczwm(vo.getWfjczwm());
		fr.setYhm(vo.getYhm());
		fr.setZjhm(vo.getZjhm());
		fr.setZjje(vo.getZjje());
		
		return "tzmx";
	}

	/**
	 * 撤单
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String chedan() throws IllegalAccessException, InvocationTargetException {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		
		// TODO 启动事务
		tzjlBo.cheDan(fr.getDzh());
		Double money = bankBackBo.cheDan(crtUser,fr.getDzh());
		
		crtUser.setUermoney(crtUser.getUermoney()+money);
		crtUser.setAllmoney(crtUser.getAllmoney()+money);
		usersBo.update(crtUser);
		
		getSession().setAttribute("userinfo", crtUser);
		
		fr.setNfcd(0);
		
		return loadPage();
	}
}
